package com.example.demoannotation.controller;

import com.example.demoannotation.annotation.LoginUser;
import com.example.demoannotation.bean.LoginUserBean;
import com.example.demoannotation.service.HostHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author yanjl
 * @ClassName LoginController
 * @Description TODO
 * @Date: 2023/6/14 17:02
 */

@RestController
@Slf4j
public class LoginController {

    @Resource
    private HostHolder hostHolder;

    /**
     * 新增字典明细
     *
     * @param loginUser
     * @return
     */
    @PostMapping("/test")
    public LoginUserBean addNewDetail(@LoginUser(isFull = true) LoginUserBean loginUser) {
        log.info("user info::{}", loginUser.toString());
        return loginUser;
    }

    /**
     * 登录保存信息倒ThreadLocal
     *
     * @param
     * @return
     */
    @GetMapping("/login2")
    public LoginUserBean loginToLocal(String name, String pwd) {

        LoginUserBean user = new LoginUserBean(name,pwd);

        hostHolder.setUser(user);
        log.info("add detail params:{}", user);
        return user;
    }

    /**
     * 从ThreadLocal获取用户信息
     *
     * @param
     * @return
     */
    @GetMapping("/test2")
    public LoginUserBean getUser2(@LoginUser LoginUserBean userBean) {

        log.info("user info:{}", userBean);
        return userBean;
    }
}